<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\service;

use app\api\model\Collect as CollectModel;
use app\api\service\User as UserService;
use app\common\service\BaseService;
use cores\exception\BaseException;
use app\common\library\helper;
use app\api\model\Goods as GoodsModel;
/**
 * 用户服务类
 * Class User
 * @package app\api\service
 */
class Collect extends BaseService
{
   
    // 当前我收藏的商品信息
 
    public function getCollectList(array $param = [],bool $isGoodsGradeMoney = true){
        // 获取当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        $model = new CollectModel;
        $param['userId'] = $userId;
         // 收藏列表
        $collectlist = $model->getCollectList($param);
        $collectlist  =$collectlist->toArray();
        if($collectlist){
            // 整理商品ID集
            $goodsIds = helper::getArrayColumn($collectlist['data'], 'goods_id');
            if (!empty($goodsIds)){
                // 获取商品图片详情列表
                $goodsList = $this->getGoodsListByIds($goodsIds, $isGoodsGradeMoney);
                $newGoodsList = array();
                foreach($goodsList as $k=>$v){
                    $newGoodsList[$v->goods_id] = $v;
                }
                // // 整理购物车商品列表
                foreach ($collectlist['data'] as $collectIdx => $item) {
                    // 查找商品, 商品不存在则删除该购物车记录
                    $collectlist['data'][$collectIdx]['goods_image'] = $newGoodsList[$item['goods_id']]['goods_image'];
                    $collectlist['data'][$collectIdx]['goods_sales'] = $newGoodsList[$item['goods_id']]['goods_sales'];
                    $collectlist['data'][$collectIdx]['is_user_grade'] = $newGoodsList[$item['goods_id']]['is_user_grade'];
                    $collectlist['data'][$collectIdx]['goods_price_old_min'] = $newGoodsList[$item['goods_id']]['goods_price_old_min'];
                    $collectlist['data'][$collectIdx]['goods_price_old_max'] = $newGoodsList[$item['goods_id']]['goods_price_old_max'];
                }

            }
        }
        
        return $collectlist;
    }

    //点击收藏商品
    public function setCollectList($parms){
        // 获取当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        $model = new CollectModel;
        if($parms['type'] == 1){
            if (!$model->add($userId,$parms['goodsId'])) {
                $this->error = '收藏失败,已收藏无需重复收藏';
                return false;
            }
        }else{
            if (!$model->clear($userId,$parms['goodsId'])) {
                $this->error = '取消收藏失败,未查到收藏记录';
                return false;
            }
        }
       
        return true;
    }


      /**
     * 根据商品ID集获取商品列表
     * @param array $goodsIds
     * @param bool $isGoodsGradeMoney 是否设置会员折扣价
     * @return mixed
     */
    private function getGoodsListByIds(array $goodsIds, bool $isGoodsGradeMoney = true)
    {
        $model = new GoodsModel;
        return $model->isGoodsGradeMoney($isGoodsGradeMoney)
            ->getListByIdsFromApi($goodsIds)
            ->hidden(GoodsModel::getHidden(['goods_images']));
    }



    /**
     * 根据商品ID验证当前用户是否收藏过
     * @param array $goodsIds
     * @param bool $isGoodsGradeMoney 是否设置会员折扣价
     * @return mixed
     */
    public function verifyCollect(int $goodsId, $php_token){
        if($php_token != null && $php_token != "[object Boolean]"){
            $userId = UserService::getCurrentLoginUserId();
            $model = new CollectModel;
           $rest = $model->getCollectInfo($userId,$goodsId);
           $rest = $rest->toArray();
            if($rest){
                return 1;
            }else{
                return 0;
            }
        }else{
            return 0;
        }


    }
 
}
